<?php

class Translation extends Eloquent {
	protected $table;
	public $timestamps = false;
	
	/********************************************************************************
	 ******************************** PUBLIC FUNCTIONS ******************************
	 ********************************************************************************/
	
	/**
	 * ses the table for the model
	 */
	public function __construct() {
		$this->table = Helper::getDB ('core') . 'translation';
		parent::__construct();
	}
	
	
	/**
	 * returns the translation from pivot table
	 * @param 		string 		$isoCode
	 * @param		bool		$useStandardLanguage		if is true and the translation is null, get the translation for standard language, else ''
	 * @return 		string
	 */
	public function getTranslation($languageId = null, $useStandardLanguage = true){
		$stdLanguageId = Config::get('app.language_id');
		$languageId = ($languageId == null) ? $stdLanguageId : $languageId;
		
		$translation = $this->languages()->where('language_id', $languageId)->first();
		if ($translation != null)
			$text = $translation->pivot->text;
		else if ($useStandardLanguage)
			$text = $this->languages()->where('language_id', $stdLanguageId)->first()->pivot->text;
		else
			$text = '';
		return $text;
	}
	
	
	/**********************************************************************
	 ************************** HAS MANY RELATIONS ************************
	 **********************************************************************/
	
	/**
	 * returns texts for all languages
	 * @return		array
	 */
	public function languages() {
		return $this->belongsToMany('Language', (Helper::getDB('core') . 'translationtext'), 'translation_id', 'language_id')->withPivot('text');
	}
	
	/**********************************************************************
	 ************************* BELONGS TO RELATIONS ***********************
	 **********************************************************************/
	
}